package co.id.vivastor.pantau;

import co.id.vivastor.pantau.util.Global;
import co.id.vivastor.pantau.util.SMSSender;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class HasilActivity extends Activity {
	public static final String IDENTIFIER = "HASIL";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hasil);
		_setOtherViews();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_hasil, menu);
		return true;
	}
	
	private void _setOtherViews() {
		//Icon Clickable goto home menu:
		ImageView icon = (ImageView) findViewById(R.id.imageView1);
		Global.setImageViewButtonListener(icon, HasilActivity.this, MainActivity.class);
		
		//Listener Send Button
		Button btnSendSMS = (Button) findViewById(R.id.buttonSend);
		btnSendSMS.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//Cek setting udah terisi:
				String kodeWilayah = Global.getData(getApplicationContext(), Global.KODE_WILAYAH, Global.DEFAULT_VALUE);
				String nomorTPS = Global.getData(getApplicationContext(), Global.NOMOR_TPS, Global.DEFAULT_VALUE);
				String SMSCenterNo = Global.getData(getApplicationContext(), Global.SMS_CENTER_NO, Global.DEFAULT_VALUE);
				
				if (kodeWilayah == Global.DEFAULT_VALUE || nomorTPS == Global.DEFAULT_VALUE || SMSCenterNo == Global.DEFAULT_VALUE || 
						kodeWilayah == "" || nomorTPS == "" || SMSCenterNo == "") {
					//Toast.makeText(getApplicationContext(), "Maaf, Setting belum terkonfigurasi", Toast.LENGTH_SHORT).show();
					Global.showDialog(HasilActivity.this, "Konfigurasi", "Maaf, setting belum terkonfigurasi. Cek halaman Setting.", "Setting");
				} else {
					//Isi Field:
					EditText editTextNasdem = (EditText) findViewById(R.id.editText_nasdem);
					EditText editTextPKB = (EditText) findViewById(R.id.editText_PKB);
					EditText editTextPKS = (EditText) findViewById(R.id.editText_PKS);
					EditText editTextPDIP = (EditText) findViewById(R.id.editText_PDIP);
					EditText editTextGolkar = (EditText) findViewById(R.id.editText_Golkar);
					EditText editTextGerindra = (EditText) findViewById(R.id.editText_gerindra);
					EditText editTextDemokrat = (EditText) findViewById(R.id.editText_demokrat);
					EditText editTextPAN = (EditText) findViewById(R.id.editText_PAN);
					EditText editTextPPP = (EditText) findViewById(R.id.editText_PPP);
					EditText editTextHanura = (EditText) findViewById(R.id.editText_hanura);
					EditText editTextAbstain = (EditText) findViewById(R.id.editText_abstain);
					EditText editTextTidakSah = (EditText) findViewById(R.id.editText_tdk_sah);
					
					int nasdem = Integer.parseInt(editTextNasdem.getText().toString());
					int PKB = Integer.parseInt(editTextPKB.getText().toString());
					int PKS = Integer.parseInt(editTextPKS.getText().toString());
					int PDIP = Integer.parseInt(editTextPDIP.getText().toString());
					int golkar = Integer.parseInt(editTextGolkar.getText().toString());
					int gerindra = Integer.parseInt(editTextGerindra.getText().toString());
					int demokrat = Integer.parseInt(editTextDemokrat.getText().toString());
					int PAN = Integer.parseInt(editTextPAN.getText().toString());
					int PPP = Integer.parseInt(editTextPPP.getText().toString());
					int hanura = Integer.parseInt(editTextHanura.getText().toString());
					int abstain = Integer.parseInt(editTextAbstain.getText().toString());
					int tidakSah = Integer.parseInt(editTextTidakSah.getText().toString());
							
					SMSSender smsSender = new SMSSender(HasilActivity.this);
					String message = HasilActivity.IDENTIFIER+" "+kodeWilayah+" "+nomorTPS+" "+
									nasdem+" "+PKB+" "+PKS+" "+PDIP+" "+golkar+" "+gerindra+" "+demokrat+" "+
									PAN+" "+PPP+" "+hanura+" "+abstain+" "+tidakSah+" ";
					smsSender.sendSMS(SMSCenterNo, message);
				}
			}
		});
	}

}
